<template>
  <div>
    <el-dialog
      title="课程查看"
      :close-on-click-modal="false"
      :visible.sync="visible"
      :before-close="handleClose"
    >
      <el-form label-width="150px">
        <el-form-item label="课程名称">
          {{dataForm.chapterName}}
        </el-form-item>
        <el-form-item label="课程封面">
          <img :src="dataForm.courseCoverUrl" class="avatar">
        </el-form-item>
        <el-form-item label="视频时长">
          {{dataForm.video_duration}}
        </el-form-item>
        <el-form-item label="上传视频">
          <video
            v-if="dataForm.sourceInfo.video_url"
            :src="dataForm.sourceInfo.video_url"
            style="width: 185px;"
            controls="controls"
            id="myvideo"
          >您的浏览器不支持视频播放
          </video>
          <div style="color:#00f;width: 100%;overflow: hidden; " v-if="dataForm.sourceInfo.video_url!=''">
            {{dataForm.sourceInfo.video_name}}
          </div>
        </el-form-item>
        <el-form-item label="视频封面">
          <img :src="dataForm.sourceInfo.cover_url" class="avatar">
        </el-form-item>
        <el-form-item label="课程内容" class="content1">
          <div v-html="dataForm.content"></div>
        </el-form-item>
        <el-form-item label="上架时间">
          {{dataForm.isPublic==1?'立即上架':'定时上架'+'—上架时间：'+dataForm.publicTime}}
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="getContent()">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :visible.sync="qrcode" width="30%">
      <div class="qrcode">
        <img :src="qrimg">
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss">
  .ueditor-box {
    height: 150px;
    // width: 60%;
  }

  .qrcode {
    width: 200px;
    height: 200px;
    margin: 0 auto;
  }

  .qrcode img {
    width: 100%;
    height: 100%;
  }
  .content1 p{
    margin: 0;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
  import ueditor from "ueditor";

  export default {
    data() {
      return {
        visible: false,
        qrcode: false,
        dataForm: {
          sourceInfo: {}
        },
        qrimg: "", //二维码
        getQr: "",
        videourl: "", //视频url
        imageUrl: '',//图片路径
        uploadUrl: "",//上传图地址
      };
    },
    mounted() {
    },
    methods: {
      // 关闭窗口音频就关闭
      handleClose() {
        this.visible = false;
      },
      // 预览
      preview() {
        this.ue.ready(() => {
          this.ueContent = this.ue.getContent();
        });
        this.getQr = this.ueContent.replace(
          /<img/g,
          "<img style='width:100%;height:auto'"
        );
        let video = ''
        if (this.dataForm.sourceInfo.video_url) {
          video = `<div><video
            src="${this.dataForm.sourceInfo.video_url}"
            poster="${this.dataForm.sourceInfo.cover_url}"
            style="width: 100%;"
            controls="controls"
          ></video></div>`
        }
        let script = `\<script\>
function setspeed(self,speed){
let btn = document.getElementsByClassName('btn')
for(let i=0;i<btn.length;i++){
btn[i].style.background='#fff'
btn[i].style.color='#000'
}
self.style.background = '#17B3A3'
self.style.color = '#fff'
document.getElementById('audiodom').playbackRate = speed
}
\<\/script\>`
        this.$http({
          url: this.$http.adornUrl("/course/bookchapter/getQr"),
          method: "post",
          data: this.$http.adornData({
            content: video + this.getQr + script
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.qrcode = true;
            this.qrimg = data.qrUrl;
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      init(bookId, id) {
        this.dataForm.id = id
        this.dataForm.bookId = bookId
        this.visible = true;
        this.$nextTick(() => {
          this.$http({
            url: this.$http.adornUrl(
              `/course/bookchapter/collegeCourseInfo/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.chapterName = data.courseInfo.chapterName;
              this.dataForm.courseCoverUrl = data.courseInfo.courseCoverUrl;
              this.dataForm.chapterDesc = data.courseInfo.chapterDesc;
              this.dataForm.sourceInfo = data.courseInfo.sourceInfo;
              this.dataForm.wordNumber = data.courseInfo.wordNumber;
              this.dataForm.chapterVisit = data.courseInfo.chapterVisit;
              this.dataForm.isPublic = data.courseInfo.isPublic;
              this.dataForm.publicTime = data.courseInfo.publicTime;
              this.dataForm.sourceInfo = this.dataForm.sourceInfo ? JSON.parse(this.dataForm.sourceInfo) : {};
              if (this.dataForm.sourceInfo && this.dataForm.sourceInfo.video_duration) {
                const duration = this.dataForm.sourceInfo.video_duration / 1000
                const hour = parseInt(duration / 3600) < 10 ? '0' + parseInt(duration / 3600) : parseInt(duration / 3600)
                const minute = parseInt(duration % 3600 / 60) < 10 ? '0' + parseInt(duration % 3600 / 60) : parseInt(duration % 3600 / 60)
                const second = parseInt(duration % 60) < 10 ? '0' + parseInt(duration % 60) : parseInt(duration % 60)
                this.dataForm.video_duration = `${hour}:${minute}:${second}`
              }
              this.dataForm.content = data.courseInfo.content;
            }
          });
        });
      },
    }
  };
</script>
